package com.huaqin.excel.handler;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;

/**
 * @author guojianhui checklist data source
 * 
 */
public class ExcelDataSource {

	// private ExcelDataHandler mHandler = null;

	private CommonFileHandler mCommonFileHandler = null;

	private String mGmsApkInfo = null;

	private static String absPath = null;
	
	private static int installApkTime = 0;

	// get project rootPath
	public static String getPath() {
		File files = new File("");

		absPath = files.getAbsolutePath();

		return absPath;
	}

	public enum DATA_SOURCE {
		PROP, DF, TXT
	}

	public enum CHECKLIST_TYPE {
		PROP, FINGERPRINT, BRAND, LOCAL, CLIENTID, DF, APPINFO
	}

	public ExcelDataSource() {

		// init data source from phone or txt
		initDataSource();
	}

	public void initDataSource() {

		getDataSource("adb shell getprop", CHECKLIST_TYPE.PROP);
		getDataSource("adb shell df", CHECKLIST_TYPE.DF);
		if(installApkTime<1){
			System.out.println("install GetAppVersion apk...");
			getDataSource("adb install " + getPath() + "/GetAppVersion.apk",
				CHECKLIST_TYPE.APPINFO);
				installApkTime++;
		}
		// ExcelDataHandler.getInstance().formatGmsPackageInfo("Gms_r5.xls", 0);
		//remove file handler this 
//		CommonFileHandler.getInstance().dealExistFile(); 
	}

	/**
	 * get prop from devices
	 * 
	 * @param cmd
	 * @param type
	 * @author guojianhui
	 */
	public void getDataSource(String cmd, final CHECKLIST_TYPE source) {

		try {
			Process process = Runtime.getRuntime().exec(cmd);
			// write df & prop to txt
			switch (source) {
			case DF:
				// delete old files
				TxtDataHandler.deleteFile("df.txt");

				break;

			case PROP:
				TxtDataHandler.deleteFile("getprop.txt");

				break;

			case APPINFO:
				TxtDataHandler.deleteFile("AppInfo.txt");

				break;

			default:
				break;
			}
			// handle appinfo.txt
			if (CHECKLIST_TYPE.APPINFO.equals(source)) {

				// sleep to wait install
				try {

					Thread.sleep(2000);

					process = Runtime
							.getRuntime()
							.exec("adb shell am start -n com.huawei.getappversion/com.huawei.getappversion.MainActivity");

					Thread.sleep(3000);
					System.out.println("pull Appinfo ");
					process = Runtime.getRuntime().exec(
							"adb pull sdcard/AppInfo.txt " + getPath());

					Thread.sleep(1000);

				} catch (Exception e) {
					e.printStackTrace();
				}

			} else {

				final BufferedWriter outputStream = new BufferedWriter(
						new OutputStreamWriter(process.getOutputStream()));
				final BufferedReader inputStream = new BufferedReader(
						new InputStreamReader(process.getInputStream()));

				final BufferedReader errorReader = new BufferedReader(
						new InputStreamReader(process.getErrorStream()));

				new Thread(new Runnable() {

					String line;

					public void run() {

//						System.out.println("listener started");
						try {

							while ((line = inputStream.readLine()) != null) {

								// System.out.println(line);
								ExcelDataHandler.getInstance().formatString(
										line, source);
								ExcelDataHandler.getInstance().formatClientId(
										line);

								switch (source) {
								case DF:

									TxtDataHandler.contentToTxt("df.txt", line);
//									System.out.println("******df_line*********"
//											+ line + "***********");
									break;

								case PROP:
									TxtDataHandler.contentToTxt("getprop.txt",
											line);
//									System.out
//											.println("******prop_line*********"
//													+ line + "***********");
									break;

								default:
									break;
								}

							}

						} catch (Exception e) {
							e.printStackTrace();
						}

					}

				}).start();

				new Thread(new Runnable() {
					final BufferedReader br = new BufferedReader(
							new InputStreamReader(System.in));

					public void run() {
//						System.out.println("writer started");

						String line;

						try {
							while ((line = br.readLine()) != null) {

								outputStream.write(line + "\r\n");
								outputStream.flush();

							}

						} catch (IOException e) {
							e.printStackTrace();
						}

					}

				}).start();
			}
			int i = process.waitFor();
//			System.out.println("i=" + i);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * @param product
	 * @return String
	 */
	public String getWCN(String product) {

		String mProduct = product;
		if (mProduct == null) {
			mProduct = "";
		}

		if (mProduct.equals("TAG-L01")) {
			return ExcelConstants.WCN_LO1;
		}

		if (mProduct.equals("TAG-L03")) {
			return ExcelConstants.WCN_LO3;
		}

		if (mProduct.equals("TAG-L13")) {
			return ExcelConstants.WCN_L13;
		}

		if (mProduct.equals("TAG-L21")) {
			return ExcelConstants.WCN_L21;
		}

		if (mProduct.equals("TAG-L22")) {
			return ExcelConstants.WCN_L22;
		}

		if (mProduct.equals("TAG-L23")) {
			return ExcelConstants.WCN_L23;
		}

		if (mProduct.equals("TAG-L32")) {
			return ExcelConstants.WCN_L32;
		}

		if (mProduct.equals("TAG-AL00")) {
			return ExcelConstants.WCN_AL00;
		}

		if (mProduct.equals("TAG-CL00")) {
			return ExcelConstants.WCN_CL00;
		}

		if (mProduct.equals("TAG-TL00")) {
			return ExcelConstants.WCN_TL00;
		}

		return mProduct;

	}

	/*
	 * match is internal product
	 * 
	 * @param product
	 * 
	 * @return boolean
	 */
	public boolean isInternalProduct(String product) {

		String mProduct = product;

		if (mProduct == null) {
			return false;
		}
		// internal product match
		if (product.matches("TAG-AL00") || product.matches("TAG-CL00")
				|| product.matches("TAG-TL00")) {
			return true;
		}

		return false;
	}

}

// }
